import React, { Component } from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    TouchableOpacity,
    Navigator,
    ListView,
    Alert
} from 'react-native';

var settings = require('../../settings');
var MLNavigatorBar = require('../MLNavigatorBar/MLNavigatorBar');
var MLTableCell = require('../MLTableCell/MLTableCell');
var MLActivityIndicatorView = require('../MLActivityIndicatorView/MLActivityIndicatorView');

var NewFriends = React.createClass({
    //耗时操作,网络请求
    componentDidMount(){
        this.setState({
            DrugId : this.props.DrugId,
            UsedAddressId : this.props.UsedAddressId,
        })
        //发送登录网络请求
        fetch(settings.fwqUrl + "/node/yj/addFriends/", {
            method: 'POST',
            headers: {
                'Accept': 'application/json; charset=utf-8',
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                addUserId : 103293,
                beAddUserId : 103295
            })
        })
            .then((response) => response.json())
            .then((responseJson) => {
                Alert.alert(
                    '提示:',
                    '成功',
                    [
                        {text: '确定'}
                    ]
                )
            })
            .catch((error) => {//错误
                //移除等待,弹出错误
                this.setState({animating:false});
                //错误
                Alert.alert(
                    '提示:',
                    '请检查您的网络',
                    [
                        {text: '确定'}
                    ]
                )

            });
    },
    getInitialState() {

        var tableData = [];

        //判断用户类别
        tableData.push('研究方案信息')
        tableData.push('用户手册')

        //ListView设置
        var ds = new ListView.DataSource({rowHasChanged:(r1, r2) => r1 !== r2});
        return {
            //ListView设置
            dataSource: ds.cloneWithRows(tableData),
            animating: true,//是否显示菊花
        }
    },

    render() {
        if (this.state.animating == true){
            return (
                <View style={styles.container}>
                    <MLNavigatorBar title={'新的朋友'} isBack={true} backFunc={() => {
                        this.props.navigator.pop()
                    }}/>
                    {/*设置完了加载的菊花*/}
                    <MLActivityIndicatorView />
                </View>

            );
        }else{
            return (
                <View style={styles.container}>
                    <MLNavigatorBar title={'新的朋友'} isBack={true} backFunc={() => {
                        this.props.navigator.pop()
                    }}/>
                    <ListView
                        dataSource={this.state.dataSource}//数据源
                        renderRow={this.renderRow}
                    />
                </View>
            );
        }
    },

    //返回具体的cell
    renderRow(rowData){
        return(
            <TouchableOpacity onPress={()=>{
                //设置数据

            }}>
                <MLTableCell title={rowData}/>
            </TouchableOpacity>
        )
    },
});


const styles = StyleSheet.create({
    container: {
        flex: 1,
    }
});

// 输出组件类
module.exports = NewFriends;
